(function () {
    var $ax = function (url, success, error) {
        this.url = url;
        this.type = "post json";
        this.data = {};
        this.dataType = "json";
        this.async = true;
        this.success = success;
        this.error = error;
        this.headers = null;
        this.load = false;
        this.index = 0;
    };

    $ax.prototype = {
        init: function () {
            var me = this;
            this.openLoadHandler = function () {
                me.index = layer.load(1, {shade: 0.1});
            };
            this.closeLoadHandler = function () {
                layer.close(me.index);
            };
            if (typeof this.error == 'undefined') {
                this.error = function (rdata) {
                    if (typeof rdata != 'undefined' && typeof rdata.responseJSON != "undefined" && typeof rdata.responseJSON.message != 'undefined')
                        Dawn.error(rdata.responseJSON.message);
                    else
                        Dawn.error("数据请求异常!")
                }
            }
        },
        createParams: function () {
            var me = this;
            let params = {
                type: this.type,
                url: Dawn.api + this.url,
                dataType: this.dataType,
                async: this.async,
                data: this.data,
                beforeSend: function (data) {
                    if (me.load) {
                        me.openLoadHandler();
                    }
                },
                headers: {},
                success: function (data) {
                    if (data.code === -3) {
                        $.removeCookie("Authorization");
                        localStorage.removeItem("Authorization");
                        localStorage.removeItem("account");
                        window.location.href = Dawn.ctxPath + "/login.html";
                        return;
                    }
                    if (data.code === -2) {
                        Dawn.error(data.message);
                        return;
                    }
                    if (data.code === 0) {
                        data.success = true;
                    } else {
                        data.success = false;
                    }
                    me.success(data);
                },
                error: function (data) {
                    me.error(data);
                },
                complete: function () {
                    if (me.load) {
                        me.closeLoadHandler();
                    }
                }
            }
            let token = $.cookie("Authorization");
            if (typeof token != "undefined" && token != null && $.trim(token) != '') {
                params.headers["Authorization"] = token;
            }
            switch (this.type.toLocaleLowerCase()) {
                case "post json":
                    params.type = "post";
                    params.contentType = "application/json";
                    params.data = JSON.stringify(this.data);
                    break;
                default:
                    break;
            }
            if (this.headers != null) {
                for (let name in this.headers) {
                    params.headers[name] = this.headers[name];
                }
            }
            return params;
        },
        start: function () {
            this.init();
            $.ajax(this.createParams());
        },
        setLoad: function (load) {
            this.load = load;
            return this;
        }
        ,
        set: function (key, value) {
            if (typeof key == "object") {
                for (var i in key) {
                    if (typeof i == "function")
                        continue;
                    this.data[i] = key[i];
                }
            } else {
                this.data[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
            }
            return this;
        },

        setHeaders: function (headers) {
            this.headers = headers;
            return this;
        },

        setData: function (data) {
            this.data = data;
            return this;
        },
        setType: function (type) {
            this.type = type;
          return this;
        },
        clear: function () {
            this.data = {};
            return this;
        },
        setAsync: function (async) {
            this.async = async;
            return this;
        },
        setSuccess: function (success) {
            this.success = success;
            return this;
        },
        setError: function (error) {
            this.error = error;
            return this;
        }
    };

    window.$ax = $ax;

    window.$request = {
        get: function (url, success, error) {
            let ajax = new $ax(url, success, error);
            ajax.type = "get";
            return ajax;
        },
        post: function (url, success, error) {
            let ajax = new $ax(url, success, error);
            ajax.type = "post";
            return ajax;
        },
        postJson: function (url, success, error) {
            return new $ax(url, success, error);
        }
    }

}());
